<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>慢查询及sql优化 | Introduction</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../home/part3/Python连接MySQL.html" />
    
    
    <link rel="prev" href="../../home/part2/6.MySQL日志及数据恢复.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.6"
        data-chapter-title="慢查询及sql优化"
        data-filepath="home/part2/7.慢查询及sql优化.md"
        data-basepath="../.."
        data-revision="Sun Oct 21 2018 16:32:53 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="home/MySQL介绍.html">
            
                
                    <a href="../../home/MySQL介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        MySQL介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="home/part1/MySQL基础.html">
            
                
                    <a href="../../home/part1/MySQL基础.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Mysql基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="home/part1/1.MySQL基础操作.html">
            
                
                    <a href="../../home/part1/1.MySQL基础操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        MySQL基础操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="home/part1/2.MySQL数据类型.html">
            
                
                    <a href="../../home/part1/2.MySQL数据类型.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        MySQL数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="home/part1/3.MySQL建库建表.html">
            
                
                    <a href="../../home/part1/3.MySQL建库建表.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        MySQL建库建表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="home/part1/4.DML操作：增,删,改.html">
            
                
                    <a href="../../home/part1/4.DML操作：增,删,改.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        DML操作：增,删,改
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="home/part1/5.DQL操作：数据查询.html">
            
                
                    <a href="../../home/part1/5.DQL操作：数据查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        DQL操作：数据查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="home/part1/6.数据导入和导出.html">
            
                
                    <a href="../../home/part1/6.数据导入和导出.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        数据导入和导出
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="home/part2/MySQL高级.html">
            
                
                    <a href="../../home/part2/MySQL高级.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        MySQL高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="home/part2/1.MySQL的多表联查.html">
            
                
                    <a href="../../home/part2/1.MySQL的多表联查.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        MySQL的多表联查
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="home/part2/2.MySQL的事务处理.html">
            
                
                    <a href="../../home/part2/2.MySQL的事务处理.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        MySQL的事务处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="home/part2/3.MySQL的视图及其它.html">
            
                
                    <a href="../../home/part2/3.MySQL的视图及其它.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        MySQL的视图及其它
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="home/part2/4.MySQL的触发器.html">
            
                
                    <a href="../../home/part2/4.MySQL的触发器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        MySQL的触发器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="home/part2/6.MySQL日志及数据恢复.html">
            
                
                    <a href="../../home/part2/6.MySQL日志及数据恢复.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        MySQL日志及数据恢复
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.6" data-path="home/part2/7.慢查询及sql优化.html">
            
                
                    <a href="../../home/part2/7.慢查询及sql优化.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        慢查询及sql优化
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="home/part3/Python连接MySQL.html">
            
                
                    <a href="../../home/part3/Python连接MySQL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        Python连接MySQL
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="home/part3/1.CRUD.html">
            
                
                    <a href="../../home/part3/1.CRUD.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        CRUD
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Introduction</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x6162;&#x67E5;&#x8BE2;&#x53CA;sql&#x4F18;&#x5316;">&#x6162;&#x67E5;&#x8BE2;&#x53CA;sql&#x4F18;&#x5316;</h1>
<hr>
<h2 id="sql&#x4F18;&#x5316;">SQL&#x4F18;&#x5316;</h2>
<pre><code class="lang-sql">&#x4F18;&#x5316;sql&#x6B65;&#x9AA4;&#xFF1A;

1. &#x6162;&#x67E5;&#x8BE2;&#x65E5;&#x5FD7;

2. &#x627E;&#x51FA;&#x6267;&#x884C;&#x6162;&#x7684;sql&#x8BED;&#x53E5;

3. &#x8FDB;&#x884C;&#x5177;&#x4F53;&#x8BED;&#x53E5;&#x5206;&#x6790;,&#x4F18;&#x5316;&#x6216;&#x5EFA;&#x7ACB;&#x7D22;&#x5F15;
</code></pre>
<h2 id="&#x6709;&#x5173;&#x6162;&#x67E5;&#x8BE2;&#x64CD;&#x4F5C;">&#x6709;&#x5173;&#x6162;&#x67E5;&#x8BE2;&#x64CD;&#x4F5C;</h2>
<pre><code class="lang-sql">-- &#x67E5;&#x770B;&#x6709;&#x5173;mysql&#x6570;&#x636E;&#x5E93;&#x670D;&#x52A1;&#x5668;&#x7684;&#x4E00;&#x4E9B;&#x4FE1;&#x606F;
show [session|global] status;
     session:&#x5F53;&#x524D;&#x8FDE;&#x63A5;
     global&#xFF1A;&#x6570;&#x636E;&#x5E93;&#x670D;&#x52A1;&#x5668;&#x542F;&#x52A8;&#x4E4B;&#x540E;
show global status;

show status like &apos;Com_%&apos;  &#x4E00;&#x822C;&#x67E5;&#x770B;&#x4EE5;com&#x5F00;&#x5934;&#x7684;

// &#x6839;&#x636E;&#x6267;&#x884C;&#x7684;sql&#x8BED;&#x53E5;&#xFF08;&#x6B21;&#x6570;&#xFF09;
Com_select:&#x67E5;&#x8BE2;
Com_update:&#x4FEE;&#x6539;&#x6B21;&#x6570;
Com_insert:&#x63D2;&#x5165;&#x6B21;&#x6570;
Com_delete:&#x5220;&#x9664;&#x7684;&#x6B21;&#x6570;

// &#x6B64;&#x5904;&#x662F;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;&#xFF08;&#x6BD4;&#x5982;&#x67E5;&#x8BE2;&#x4E00;&#x6B21;&#x663E;&#x793A;20&#x884C;&#xFF0C;&#x90A3;&#x4E48;&#x884C;&#x6570;&#x589E;&#x52A0;20&#x884C;&#xFF09;
InnoDB_rows_read:&#x6267;&#x884C;select&#x64CD;&#x4F5C;&#x7684;&#x6B21;&#x6570; 
InnoDB_rows_updated:&#x6267;&#x884C;update&#x7684;&#x6B21;&#x6570;
InnoDB_rows_inserted:&#x6267;&#x884C;insert&#x64CD;&#x4F5C;&#x7684;&#x6B21;&#x6570;
InnoDB_rows_deleted:&#x6267;&#x884C;delete&#x64CD;&#x4F5C;&#x7684;&#x6B21;&#x6570;

connections&#xFF1A;&#x8FDE;&#x63A5;mysql&#x7684;&#x6570;&#x91CF;
Uptime:&#x670D;&#x52A1;&#x5668;&#x5DF2;&#x7ECF;&#x5DE5;&#x4F5C;&#x7684;&#x79D2;&#x6570;

Slow_queries:&#x6162;&#x67E5;&#x8BE2;&#x7684;&#x6B21;&#x6570;




-- // &#x67E5;&#x770B;&#x201C;&#x6162;&#x67E5;&#x8BE2;&#x201D;&#x7684;&#x914D;&#x7F6E;&#x4FE1;&#x606F;
show variables like &quot;%slow%&quot;; 
 -- // &#x67E5;&#x770B;&#x201C;&#x6162;&#x67E5;&#x8BE2;&#x201D;&#x7684;&#x65F6;&#x95F4;&#x5B9A;&#x4E49;
show variables like &quot;long%&quot;;  

 -- //&#x8BBE;&#x7F6E;&#x201C;&#x6162;&#x67E5;&#x8BE2;&#x201D;&#x7684;&#x65F6;&#x95F4;&#x5B9A;&#x4E49;
set long_query_time=0.2;   
-- //&#x5F00;&#x542F;&#x6162;&#x65E5;&#x5FD7;
set global slow_query_log=&apos;ON&apos;;



mysql&gt; show variables like &apos;%slow%&apos;;
+---------------------------+--------------------------------------------+
| Variable_name             | Value                                      |
+---------------------------+--------------------------------------------+
| log_slow_admin_statements | OFF                                        |
| log_slow_slave_statements | OFF                                        |
| slow_launch_time          | 2                                          |
| slow_query_log            | OFF                                        |
| slow_query_log_file       | /var/lib/mysql/yc-virtual-machine-slow.log |
+---------------------------+--------------------------------------------+


mysql&gt; show variables like &quot;long%&quot;;  
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
</code></pre>
<h2 id="&#x5B9A;&#x4F4D;&#x6267;&#x884C;&#x6548;&#x7387;&#x8F83;&#x4F4E;&#x7684;sql&#x8BED;&#x53E5;">&#x5B9A;&#x4F4D;&#x6267;&#x884C;&#x6548;&#x7387;&#x8F83;&#x4F4E;&#x7684;SQL&#x8BED;&#x53E5;</h2>
<pre><code class="lang-sql">1. explain select * from table where id=1000;
2. desc select * from table where id=1000;
 *************************** 1. row ***************************
        id: 1
select_type: SIMPLE(&#x7B80;&#x5355;&#x67E5;&#x8BE2;&#xFF0C;&#x4E0D;&#x662F;&#x591A;&#x8868;&#x67E5;&#x8BE2;&#x548C;&#x590D;&#x6742;&#x67E5;&#x8BE2;) &#x53EF;&#x80FD;&#x7684;&#x503C;&#xFF1A;simple,primary,union,dependent union,union result
     table: php114&#xFF08;&#x8868;&#x540D;&#xFF09;
      type: ALL&#xFF08;&#xFF09;     &#x53EF;&#x80FD;&#x7684;&#x503C;&#xFF1A;system,const(&#x6700;&#x4F18;&#x5316;&#xFF0C;&#x4F7F;&#x7528;&#x4E86;&#x4E3B;&#x952E;&#x6216;&#x8005;&#x552F;&#x4E00;&#x5065;),eq_ref,ref.ref_or_null,index_merge
possible_keys: NULL         &#x63D0;&#x793A;&#x4F7F;&#x7528;&#x54EA;&#x4E2A;&#x7D22;&#x5F15;&#x4F1A;&#x5728;&#x8BE5;&#x8868;&#x4E2D;&#x627E;&#x5230;&#x884C;
       key: NULL&#xFF08;&#xFF09;     mysql&#x4F7F;&#x7528;&#x7684;&#x7D22;&#x5F15;&#xFF0C;&#x7B80;&#x5355;&#x4E14;&#x91CD;&#x8981;
   key_len: NULL&#xFF08;&#xFF09;     mysql&#x4F7F;&#x7528;&#x7684;&#x7D22;&#x5F15;&#x957F;&#x5EA6;
       ref: NULL          &#x663E;&#x793A;&#x4F7F;&#x7528;&#x54EA;&#x4E2A;&#x5217;&#x6216;&#x8005;&#x5E38;&#x6570;&#x4E0E;key&#x4E00;&#x8D77;&#x4ECE;&#x8868;&#x4E2D;&#x9009;&#x62E9;&#x884C;
      rows: 6          mysql&#x6267;&#x884C;&#x67E5;&#x8BE2;&#x7684;&#x884C;&#x6570;&#xFF0C;&#x7B80;&#x5355;&#x4E14;&#x91CD;&#x8981;&#xFF0C;&#x6570;&#x503C;&#x8D8A;&#x5927;&#x8D8A;&#x4E0D;&#x597D;&#xFF0C;&#x8BF4;&#x660E;&#x6CA1;&#x6709;&#x7528;&#x597D;&#x7D22;&#x5F15;
     Extra:          &#x5305;&#x542B;mysql&#x89E3;&#x51B3;&#x67E5;&#x8BE2;&#x7684;&#x8BE6;&#x7EC6;&#x4FE1;&#x606F;
1 row in set (0.00 sec)
</code></pre>
<h2 id="sql&#x8BED;&#x53E5;&#x5177;&#x4F53;&#x4F18;&#x5316;">sql&#x8BED;&#x53E5;&#x5177;&#x4F53;&#x4F18;&#x5316;</h2>
<pre><code class="lang-sql">1. &#x7ED9;&#x7ECF;&#x5E38;&#x4F5C;&#x4E3A;&#x67E5;&#x8BE2;&#x6761;&#x4EF6;&#x7684;&#x5B57;&#x6BB5;&#x6DFB;&#x52A0;&#x7D22;&#x5F15;

2. &#x6DFB;&#x52A0;&#x7D22;&#x5F15;&#x7684;&#x5B57;&#x6BB5;,&#x5C3D;&#x91CF;&#x4FDD;&#x6301;&#x4E00;&#x5B9A;&#x7684;&#x552F;&#x4E00;&#x6027;,

3. &#x5728;&#x4F7F;&#x7528;&#x7D22;&#x5F15;&#x65F6;,&#x6CE8;&#x610F;&#x7C7B;&#x578B;,&#x9632;&#x6B62;&#x51FA;&#x73B0;&#x6709;&#x7D22;&#x5F15;,&#x7528;&#x4E0D;&#x4E0A;

4. &#x6CE8;&#x610F;&#x5728;&#x4F7F;&#x7528;like&#x6A21;&#x7CCA;&#x67E5;&#x8BE2;&#x65F6;,&#x5982;&#x679C;%&#x6216;_&#x51FA;&#x73B0;&#x5728;&#x524D;&#x9762;,&#x5219;&#x7528;&#x4E0D;&#x4E0A;&#x7D22;&#x5F15;
</code></pre>
<p><strong>&#x6CE8;&#x610F;:&#x7D22;&#x5F15;&#x4E0D;&#x662F;&#x52A0;&#x7684;&#x8D8A;&#x591A;&#x8D8A;&#x597D;,&#x540C;&#x65F6;&#x7B26;&#x5408;1,2,&#x6761;&#x4EF6;&#x5219;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x7D22;&#x5F15;</strong>
<strong>&#x7D22;&#x5F15;&#x8FC7;&#x591A;,&#x4F1A;&#x589E;&#x52A0;&#x8868;&#x7684;&#x7EF4;&#x62A4;&#x6210;&#x672C;,&#x589E; &#x5220; &#x6539; &#x90FD;&#x4F1A;&#x53D8;&#x6162;</strong></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../home/part2/6.MySQL日志及数据恢复.html" class="navigation navigation-prev " aria-label="Previous page: MySQL日志及数据恢复"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../home/part3/Python连接MySQL.html" class="navigation navigation-next " aria-label="Next page: Python连接MySQL"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
